﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThreeByte.ActiveDeck.Config;
using System.Xml.Linq;

namespace ThreeByte.ActiveDeck.AddIn.Config
{
    public class KioskConfiguration
    {
        private static readonly XNamespace ns = Service.ServiceConfiguration.NAMESPACE;
        public static readonly XName XML_NAME = ns + "KioskConfiguration";

        public bool Enabled { get; set; }
        public int AttractModeSection { get; set; }
        public int AttractModePage { get; set; }
        public int FirstUserSection { get; set; }
        public int FirstUserPage { get; set; }
        public TimeSpan Timeout { get; set; }
        public bool AutoUpdate { get; set; }

        public KioskConfiguration() {
            Timeout = TimeSpan.FromMinutes(2);  //Default value
            AutoUpdate = true;
        }

        public XElement ToXml() {
            XElement node = new XElement(XML_NAME, new XAttribute("Enabled", Enabled));
            if(AttractModePage != 0) {
                XElement attractNode = new XElement(ns + "AttractMode", new XAttribute("Section", AttractModeSection), new XAttribute("Page", AttractModePage));
                node.Add(attractNode);
            }
            if(FirstUserPage != 0) {
                XElement userNode = new XElement(ns + "UserMode", new XAttribute("Section", FirstUserSection), new XAttribute("Page", FirstUserPage));
                node.Add(userNode);
            }
            XElement idleNode = new XElement(ns + "IdleMessage", new XAttribute("ChannelFormat", "%@_K_%d"), new XAttribute("Message", "IdledOut"));
            node.Add(idleNode);

            node.Add(new XAttribute("Timeout", Timeout.ToString()));
            node.Add(new XAttribute("AutoUpdate", AutoUpdate));
            return node;
        }

        public static KioskConfiguration FromXml(XElement node) {
            KioskConfiguration config = new KioskConfiguration();
            config.Enabled = bool.Parse(node.Attribute("Enabled").Value);
            try {
                config.Timeout = TimeSpan.Parse(node.Attribute("Timeout").Value);
                config.AutoUpdate = bool.Parse(node.Attribute("AutoUpdate").Value);
            } catch(Exception) {
                //TODO: Log this
            }
            XElement attractNode = node.Element(ns + "AttractMode");
            if(attractNode != null) {
                config.AttractModeSection = int.Parse(attractNode.Attribute("Section").Value);
                config.AttractModePage = int.Parse(attractNode.Attribute("Page").Value);
            }

            XElement userNode = node.Element(ns + "UserMode");
            if(userNode != null) {
                config.FirstUserSection = int.Parse(userNode.Attribute("Section").Value);
                config.FirstUserPage = int.Parse(userNode.Attribute("Page").Value);
            }

            return config;
        }
    }
}
